﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.FeatureBase
{
    public partial class BaseView : FormViewBase
    {
        private object dataSource;

        public object DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }        
        public BaseView()
            :base()
        {          
            InitializeComponent();            

            InitView();

            dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);            
        }

        void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(e.RowIndex.ToString(), e.InheritedRowStyle.Font, b,
                    e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);

            }
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNew(sender, e);
        }
        public void InvisibleSearchControl()
        {
            TbxSearchInput.Visible = false;
        }        
#region Virtual Method
        public virtual void InitView()
        {
            BuildSearchMenu();
            InitDataGridView();
            return;
        }
        public virtual void AddNew(object sender, EventArgs e)
        {
            return;
        }        
        public virtual void DeleteItem(object sender, EventArgs e)
        {
            return;
        }
        public virtual void EditItem(object sender, EventArgs e)
        {
            return;
        }
        public virtual void RefreshData(object sender, EventArgs e)
        {
            return;
        }

        public virtual void DataSourceChange(object sender, EventArgs e)
        {
            return;
        }
        public virtual void LoadForm(object sender, EventArgs e)
        {
            return;
        }
        public virtual void SearchPerform(object sender, EventArgs e)
        {
            return;
        }
        public virtual void BuildSearchMenu()
        {
            return;
        }
        public virtual void InitDataGridView()
        {
            return;
        }
#endregion

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditItem(sender, e);
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            DeleteItem(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData(sender, e);
        }

        private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount>0)
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }
            else
            {
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
            }
            DataSourceChange(sender, e);
        }

        private void BaseView_Load(object sender, EventArgs e)
        {
            LoadForm(sender, e);
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            EditItem(sender, e);
            this.Cursor = Cursors.Default;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchPerform(sender, e);
        }
    }
}
